package view

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import androidx.cardview.widget.CardView
import app.AppContext
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource

import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.engine.GlideException

import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.Target
import com.github.ybq.android.spinkit.SpinKitView
import com.github.ybq.android.spinkit.SpriteFactory
import com.github.ybq.android.spinkit.Style
import com.hontech.pizzav2.R
import jp.wasabeef.glide.transformations.RoundedCornersTransformation

class LoadImageView : CardView {
    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)

    private val imageView = ImageView(context)
    private val progress = SpinKitView(context)

    init {
        val params = FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.MATCH_PARENT,
            FrameLayout.LayoutParams.MATCH_PARENT
        )
        imageView.layoutParams = params
        val params2 = FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.MATCH_PARENT,
            FrameLayout.LayoutParams.MATCH_PARENT
        )
        params2.setMargins(50, 50, 50, 50)
        progress.layoutParams = params2
        imageView.scaleType = ImageView.ScaleType.FIT_XY
        addView(progress)
        addView(imageView)
        progress.setColor(context.resources.getColor(android.R.color.holo_orange_light))
        progress.setIndeterminateDrawable(SpriteFactory.create(Style.CIRCLE))
        imageView.setImageResource(R.drawable.shape_translate)
        radius = 5f
        cardElevation = 5f
        setCardBackgroundColor(Color.WHITE)
    }

    fun setImageWithUrl(url: String) {
        imageView.setImageResource(R.drawable.shape_translate)
        progress.visibility = View.VISIBLE
        val options = RequestOptions().transform(RoundedCornersTransformation(5, 0)).error(
            R.drawable.ic_image_load_error
        ).diskCacheStrategy(
            DiskCacheStrategy.ALL
        ).skipMemoryCache(false)
        imageView.setTag(R.id.imageId, url)
        Glide.with(AppContext).load(url).apply(options)
            .listener(object : RequestListener<Drawable> {
                override fun onLoadFailed(
                    e: GlideException?, model: Any?, target: Target<Drawable>?,
                    isFirstResource: Boolean
                ): Boolean {
                    val info = imageView.getTag(R.id.imageId)
                    if (info != null && info == url) {
                        progress.visibility = View.GONE
                    }
                    return false
                }

                override fun onResourceReady(
                    resource: Drawable?, model: Any?,
                    target: Target<Drawable>?, dataSource: DataSource?,
                    isFirstResource: Boolean
                ): Boolean {
                    val info = imageView.getTag(R.id.imageId)
                    if (info != null && info == url) {
                        progress.visibility = View.GONE
                    }
                    return false
                }
            }).into(imageView)
    }
}